//
//  BaseTabBarController.swift
//  Community
//
//  Created by Vampire on 2019/7/18.
//  Copyright © 2019 ZKJ. All rights reserved.
//

import UIKit

class SWPTBaseTabBarController: UITabBarController ,RootTabBarDelegate{
    func addClick() {
        self.selectedIndex = 1
    }
    

    fileprivate lazy var tabBarView: UIView = { [unowned self] in
        let bView = UIView.init()
        bView.backgroundColor = UIColor.white
        return bView
        }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tabBarHeight = kScreenIphoneX ? 83 : 49
        tabBarView.frame = CGRect(x: 0, y: 0, width: Int(MAINWIDTH), height: tabBarHeight)
        self.tabBar.addSubview(tabBarView)
        
        self.tabBar.isTranslucent = false
   
        
        let tab = baseTabbar()
        tab.addDelegate = self
        self.setValue(tab, forKey: "tabBar")
        setVC(vcArr: ["SWPTHomeViewController","SWPTHomeSearchViewController","SWPTMainViewController"], titleArr: [NSLocalizedString("首页", comment: ""),"",NSLocalizedString("我的", comment: "")], normalImgArr: ["find_normal","tabbar_search","mine"], selectImgArr: ["find_normal","tabbar_search","mine"])
    }
    
    public func setVC(vcArr:NSArray,titleArr:NSArray,normalImgArr:NSArray,selectImgArr:NSArray) {
        
        for vc in self.viewControllers ?? [] {
            vc.removeFromParent()
        }
        var vc : UIViewController?
        for i in 0..<vcArr.count {
            //创建根控制器
            switch i {
            case 0:
                vc = SWPTHomeViewController()
            case 1:
                vc = SWPTHomeSearchViewController()
            case 2:
                vc = SWPTMainViewController()
            default:
                break
            }
            
            
            //2.更改字体颜色
            self.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor :UIColor.darkGray], for: UIControl.State.normal)
            self.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor :mainGreenColor], for: UIControl.State.selected)
            
            //设置标题
            self.addChildViewController(childVC: vc!, title: titleArr[i] as! String, norImgStr: normalImgArr[i] as! String, seleImgStr: selectImgArr[i] as! String)
        }
        
        
        
    }
    
    func addChildViewController(childVC: UIViewController,title: String,norImgStr: String,seleImgStr: String) {
        
        self.tabBarItem.title = title
        self.tabBar.tintColor = mainGreenColor
        
        childVC.title = title as String
        
        var norImg = UIImage(named: norImgStr)
        norImg = norImg?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        
        var seleImg = UIImage(named: seleImgStr)?.imageBy(tintColor: mainGreenColor)
        seleImg = seleImg?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        
        let navVC = SWPTBaseNavigationController(rootViewController: childVC)
        navVC.tabBarItem = UITabBarItem(title: title, image: norImg, selectedImage: seleImg)
        
        print(message: self.viewControllers)
        
        self.addChild(navVC)
    }
}
